package com.xrit.sunnyday.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Menu;
import com.xrit.sunnyday.pojo.Role;
import com.xrit.sunnyday.service.MenuService;
import com.xrit.sunnyday.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private UserService userService;

    @Reference
    private MenuService menuService;

    @GetMapping("/getMenu")
    public Result getMenu(){
        //1.先定义返回数据的总集合
        List<Map<String,Object>> menu = new ArrayList<>();
        //从security中获取用户名
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        //根据登录的用户名取查询用户的角色
        com.xrit.sunnyday.pojo.User user_pojo = userService.findByUsername(username);
        //取出角色
        Set<Role> roles = user_pojo.getRoles();
        for (Role role : roles) {
            //【处理1级菜单】根据角色获取的角色对应的1级菜单
            List<Menu> parentMenu = menuService.findByRoleId(role.getId());
            for (Menu parentIndb : parentMenu) {
                //初始化1级菜单对应的Map
                Map<String,Object> parentMenuMap = new HashMap<>();
                parentMenuMap.put("path",parentIndb.getPath());
                //防止后期用户重复添加菜单
                if (!menu.contains(parentMenuMap)){
                    parentMenuMap.put("title",parentIndb.getName());
                    parentMenuMap.put("icon",parentIndb.getIcon());
                    parentMenuMap.put("children",new ArrayList<>());
                    menu.add(parentMenuMap);
                }else {
                    //如果菜单已经存在，返回这个菜单的索引，获取这个菜单
                    int i = menu.indexOf(parentMenuMap);
                    parentMenuMap = menu.get(i);
                }
                //【处理2级菜单】:获取某一个1级菜单的子菜单集合
                List<Map<String,Object>> childrenMapList =
                        (List<Map<String, Object>>) parentMenuMap.get("children");
                //从某一个1级菜单中获取所有子菜单的真实数据(数据库的数据),给填充
                List<Menu> childrenInDb = parentIndb.getChildren();
                for (Menu children : childrenInDb) {
                    Map<String,Object> childrenMenuMap = new HashMap<>();
                    //防止子菜单重复
                    childrenMenuMap.put("path",children.getPath());
                    if (!childrenMapList.contains(childrenMenuMap)){
                        childrenMenuMap.put("title",children.getName());
                        childrenMenuMap.put("linkUrl",children.getLinkUrl());
                        childrenMapList.add(childrenMenuMap);
                    }
                }
            }

        }
        return new Result(true, MessageConstant.GET_MENU_SUCCESS,menu);
    }

}
